package com.yupi.yupibackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.yupibackend.exception.BusinessException;
import com.yupi.yupibackend.exception.ErrorCode;
import com.yupi.yupibackend.exception.ThrowUtils;
import com.yupi.yupibackend.model.entity.InterfaceInfo;
import com.yupi.yupibackend.model.entity.User;
import com.yupi.yupibackend.model.req.interfaceInfo.InterfaceInfoAddRequest;
import com.yupi.yupibackend.model.req.interfaceInfo.InterfaceInfoQueryRequest;
import com.yupi.yupibackend.model.req.interfaceInfo.InterfaceInfoUpdateRequest;
import com.yupi.yupibackend.model.vo.InterfaceInfoVo;
import com.yupi.yupibackend.service.InterfaceInfoService;
import com.yupi.yupibackend.mapper.InterfaceInfoMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 86178
* @description 针对表【interface_info(接口信息)】的数据库操作Service实现
* @createDate 2025-05-03 19:37:01
*/
@Service
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
    implements InterfaceInfoService{

    /**
     * 添加接口信息
     * @param interfaceInfoAddRequest
     * @return
     */
    @Override
    public Long addInterfaceInfo(InterfaceInfoAddRequest interfaceInfoAddRequest, User loginUser) {
        ThrowUtils.throwIf(interfaceInfoAddRequest == null, ErrorCode.PARAMS_ERROR);
        String name = interfaceInfoAddRequest.getName();
        String description = interfaceInfoAddRequest.getDescription();
        String url = interfaceInfoAddRequest.getUrl();
        String requestHeader = interfaceInfoAddRequest.getRequestHeader();
        String responseHeader = interfaceInfoAddRequest.getResponseHeader();
        String method = interfaceInfoAddRequest.getMethod();
        if(StrUtil.isBlank(name)|| name.length()>50){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口名称不能为空且不能超过50个字符");
        }
        if(StrUtil.isBlank(description)|| description.length()>255){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口描述不能为空且不能超过255个字符");
        }
        if(StrUtil.isBlank(url)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口地址不能为空");
        }
        if(StrUtil.isBlank(requestHeader)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求头不能为空");
        }
        if(StrUtil.isBlank(responseHeader)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"响应头不能为空");
        }
        if(StrUtil.isBlank(method)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求类型不能为空");
        }
        InterfaceInfo interfaceInfo = new InterfaceInfo();
        //2.参数填充
        interfaceInfo.setName(name);
        interfaceInfo.setDescription(description);
        interfaceInfo.setUrl(url);
        interfaceInfo.setRequestHeader(requestHeader);
        interfaceInfo.setResponseHeader(responseHeader);
        interfaceInfo.setMethod(method);
        interfaceInfo.setUserId(loginUser.getId());
        //3.保存到数据库
        boolean save = this.save(interfaceInfo);
        if(!save){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"添加接口信息失败");
        }
        return interfaceInfo.getId();
    }

    /**
     * 更新接口
     * @param interfaceInfoUpdateRequest
     * @return
     */
    @Override
    public boolean updateInterfaceInfo(InterfaceInfoUpdateRequest interfaceInfoUpdateRequest) {
        ThrowUtils.throwIf(interfaceInfoUpdateRequest == null, ErrorCode.PARAMS_ERROR);
        Long id = interfaceInfoUpdateRequest.getId();
        String name = interfaceInfoUpdateRequest.getName();
        String description = interfaceInfoUpdateRequest.getDescription();
        String url = interfaceInfoUpdateRequest.getUrl();
        String requestHeader = interfaceInfoUpdateRequest.getRequestHeader();
        String responseHeader = interfaceInfoUpdateRequest.getResponseHeader();
        String method = interfaceInfoUpdateRequest.getMethod();
        if(id==null|| id<0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"id非法");
        }
        if(StrUtil.isBlank(name)|| name.length()>50){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口名称不能为空且不能超过50个字符");
        }
        if(StrUtil.isBlank(description)|| description.length()>255){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口描述不能为空且不能超过255个字符");
        }
        if(StrUtil.isBlank(url)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口地址不能为空");
        }
        if(StrUtil.isBlank(requestHeader)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求头不能为空");
        }
        if(StrUtil.isBlank(responseHeader)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"响应头不能为空");
        }
        if(StrUtil.isBlank(method)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求类型不能为空");
        }
        InterfaceInfo interfaceInfo = new InterfaceInfo();
        //2.参数填充
        interfaceInfo.setId(id);
        interfaceInfo.setName(name);
        interfaceInfo.setDescription(description);
        interfaceInfo.setUrl(url);
        interfaceInfo.setRequestHeader(requestHeader);
        interfaceInfo.setResponseHeader(responseHeader);
        interfaceInfo.setMethod(method);
        //3. 更新
        boolean update = this.updateById(interfaceInfo);
        if(!update){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"更新接口信息失败");
        }
        return true;
    }

    /**
     * 查询接口信息
     * @param interfaceInfoQueryRequest
     * @return
     */
    @Override
    public InterfaceInfoVo queryInterfaceInfo(InterfaceInfoQueryRequest interfaceInfoQueryRequest) {
        ThrowUtils.throwIf(interfaceInfoQueryRequest == null, ErrorCode.PARAMS_ERROR);
        QueryWrapper<InterfaceInfo> queryWrapper = this.getQueryWrapper(interfaceInfoQueryRequest);
        InterfaceInfo interfaceInfo = this.getOne(queryWrapper);
        return this.getInterfaceInfoVo(interfaceInfo);
    }

    /**
     * 获取查询条件
     * @param interfaceInfoQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<InterfaceInfo> getQueryWrapper(InterfaceInfoQueryRequest interfaceInfoQueryRequest) {
        ThrowUtils.throwIf(interfaceInfoQueryRequest == null, ErrorCode.PARAMS_ERROR);
        QueryWrapper<InterfaceInfo> queryWrapper = new QueryWrapper<>();
        Long id = interfaceInfoQueryRequest.getId();
        String name = interfaceInfoQueryRequest.getName();
        String description = interfaceInfoQueryRequest.getDescription();
        String url = interfaceInfoQueryRequest.getUrl();
        String requestHeader = interfaceInfoQueryRequest.getRequestHeader();
        String responseHeader = interfaceInfoQueryRequest.getResponseHeader();
        String method = interfaceInfoQueryRequest.getMethod();
        Long userId = interfaceInfoQueryRequest.getUserId();
        queryWrapper.eq(id != null && id >= 0, "id", id);
        queryWrapper.eq(StrUtil.isNotBlank(name), "name", name);
        queryWrapper.eq(StrUtil.isNotBlank(description), "description", description);
        queryWrapper.eq(StrUtil.isNotBlank(url), "url", url);
        queryWrapper.eq(StrUtil.isNotBlank(requestHeader), "requestHeader", requestHeader);
        queryWrapper.eq(StrUtil.isNotBlank(responseHeader), "responseHeader", responseHeader);
        queryWrapper.eq(StrUtil.isNotBlank(method), "method", method);
        queryWrapper.eq(userId != null && userId >= 0, "userId", userId);
        return queryWrapper;
    }

    /**
     * 获取接口信息vo
     * @param interfaceInfo
     * @return
     */
    @Override
    public InterfaceInfoVo getInterfaceInfoVo(InterfaceInfo interfaceInfo) {
        InterfaceInfoVo interfaceInfoVo = new InterfaceInfoVo();
        BeanUtil.copyProperties(interfaceInfo, interfaceInfoVo);
        return interfaceInfoVo;
    }

    /**
     * 获取当前登录用户信息列表(脱敏)
     * @param userList
     * @return
     */
    @Override
    public List<InterfaceInfoVo> getInterfaceInfoVoList(List<InterfaceInfo> interfaceInfoList) {
        if(CollectionUtil.isEmpty(interfaceInfoList)){
            return new ArrayList<>();
        }
        return interfaceInfoList.stream().map(this::getInterfaceInfoVo).collect(Collectors.toList());
    }


    /**
     * 判断接口信息是否合法
     * @param interfaceInfo
     * @return
     */
    @Override
    public void isValidInterfaceInfo(InterfaceInfo interfaceInfo,Boolean add) {
        ThrowUtils.throwIf(interfaceInfo == null, ErrorCode.PARAMS_ERROR);
        String name = interfaceInfo.getName();
        String description = interfaceInfo.getDescription();
        String url = interfaceInfo.getUrl();
        String requestHeader = interfaceInfo.getRequestHeader();
        String responseHeader = interfaceInfo.getResponseHeader();
        Integer status = interfaceInfo.getStatus();
        String method = interfaceInfo.getMethod();
        Long userId = interfaceInfo.getUserId();
        // 如果是添加，参数都不能为空
        if(add){
            if(StrUtil.isBlank(name) || StrUtil.isBlank(description) || StrUtil.isBlank(url)|| StrUtil.isBlank(requestHeader) || StrUtil.isBlank(responseHeader) || status == null || StrUtil.isBlank(method) || userId == null){
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"添加接口信息时参数不能为空");
            }
        }
        if(StrUtil.isNotBlank(name) && name.length() > 50){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口名称不能超过50个字符");
        }
        if(StrUtil.isNotBlank(description) && description.length() > 255){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口描述不能超过255个字符");
        }
    }
}




